package com.xjnt.base.support.admin.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import com.jfinal.kit.HashKit;
import com.xjnt.base.support.admin.entity.Account;
import com.xjnt.base.support.admin.entity.Org;
import com.xjnt.base.support.admin.entity.User;
import com.xjnt.base.support.admin.service.AccountService;
import com.xjnt.base.support.admin.service.OrgService;
import com.xjnt.base.support.admin.service.UserService;
import com.xjnt.frameworks.annotation.AutoInject;
import com.xjnt.frameworks.annotation.Router;
import com.xjnt.frameworks.core.CRUDController;
import com.xjnt.frameworks.exception.BusinessException;
import com.xjnt.frameworks.web.message.RetMsg;

@Router(name="/system/ucenter", view="/ucenter/user")
public class UserController extends CRUDController<User, UserService> {
	
	@AutoInject
	private UserService userService;
	
	@AutoInject
	private AccountService accountService;
	
	@AutoInject
	private OrgService orgService;
	
	@Override
	protected String getBizName() {
		return "用户资料";
	}
	
	@Override
	protected Class<User> getClazz() {
		return User.class;
	}

	@Override
	protected UserService getService() {
		return userService;
	}
	
	public void user(){
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user = getService().findById(((Account)session.getAttribute("CURR_USER")).getStr(Account.FK_USERID));
		setAttr("user", user);
		render("list.html");
	}
	
	public void account(){
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		Account account = accountService.findById(((Account)session.getAttribute("CURR_USER")).getStr(Account.PK_ID));
		Org org = orgService.findById(account.getStr(Account.FK_ORGID));
		setAttr("org", org);
		setAttr("account", account);
		render("user-account.html");
	}
	
	public void changePwd(){
		render("user-password.html");
	}
	
	/**
	 * 修改个人资料
	 */
	public void updUser() {
		RetMsg retMsg = new RetMsg();
		try {
			User user = getModel(User.class);
			boolean flag = userService.update(user);
			if(flag){
				retMsg.pushOk("保存资料成功");
			}else{
				retMsg.pushOk("保存资料失败");
			}
			renderJson(retMsg);
		} catch (Exception e) {
			throw new BusinessException("修改" + getBizName(), "发生异常：", e);
		}
	}
	
	/**
	 * 修改密码
	 */
	public void savePassword(){
		RetMsg retMsg = new RetMsg();
		try {
			Subject subject = SecurityUtils.getSubject();
			Session session = subject.getSession();
			Account account = accountService.findById(((Account)session.getAttribute("CURR_USER")).getStr(Account.PK_ID));
			String oldPwd = getPara("oldPwd");
			String newPwd = getPara("newPwd");
			if(null != account && !"".equals(account)){
				if(HashKit.md5(oldPwd).equals(account.getStr(Account.PASSWORD))){
					account.set("password", HashKit.md5(newPwd));
					boolean flag = accountService.update(account);
					if(flag){
						retMsg.pushOk("密码修改成功");
					}else{
						retMsg.pushOk("密码修改失败");
					}
				} else {
					retMsg.pushOk("您输入的原始密码错误！");
				}
			}
			renderJson(retMsg);
		} catch (Exception e) {
			throw new BusinessException("修改密码", "发生异常：", e);
		}
	}
}
